/*!
 * \file frlist_merge_header_top.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __my_list_node_free(Frlist_node *node)
{
    frfree(node->data);
    return;
}


static void __case1(void)
{
    Frlist_header *p_st_hdr_dst = NULL;
    Frlist_header *p_st_hdr_src = NULL;

    frlist_merge_header_top(p_st_hdr_dst, p_st_hdr_src);

    CU_ASSERT_PTR_NULL(p_st_hdr_dst);

}

static void __case2(void)
{
    Frlist_header *p_st_hdr_dst = NULL;
    Frlist_header *p_st_hdr_src = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;

    p_st_hdr_dst = frlist_create();
    p_st_hdr_src = frlist_create();

    p_data = (char *)frmalloc(strlen("1") + 1);
    snprintf(p_data,2,"%s","1");
    list_node = frlist_node_create(p_data);
    frlist_add_tail(p_st_hdr_dst,list_node);

    p_data = (char *)frmalloc(strlen("2") + 1);
    snprintf(p_data,2,"%s","2");
    list_node = frlist_node_create(p_data);
    frlist_add_tail(p_st_hdr_dst,list_node);

    p_data = (char *)frmalloc(strlen("3") + 1);
    snprintf(p_data,2,"%s","3");
    list_node = frlist_node_create(p_data);
    frlist_add_tail(p_st_hdr_src,list_node);

    p_data = (char *)frmalloc(strlen("4") + 1);
    snprintf(p_data,2,"%s","4");
    list_node = frlist_node_create(p_data);
    frlist_add_tail(p_st_hdr_src,list_node);

    frlist_merge_header_top(p_st_hdr_dst, p_st_hdr_src);

    CU_ASSERT_EQUAL(p_st_hdr_dst->size,4);

    list_node = frlist_node_idx(p_st_hdr_dst, 0);
    CU_ASSERT_STRING_EQUAL(list_node->data,"3");

    list_node = frlist_node_idx(p_st_hdr_dst, 1);
    CU_ASSERT_STRING_EQUAL(list_node->data,"4");

    list_node = frlist_node_idx(p_st_hdr_dst, 2);
    CU_ASSERT_STRING_EQUAL(list_node->data,"1");

    list_node = frlist_node_idx(p_st_hdr_dst, 3);
    CU_ASSERT_STRING_EQUAL(list_node->data,"2");

    frlist_destroy(p_st_hdr_dst,__my_list_node_free);
    frlist_destroy(p_st_hdr_src,__my_list_node_free);
    return;
}


static void __case3(void)
{
    Frlist_header *p_st_hdr_dst = NULL;
    Frlist_header *p_st_hdr_src = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;

    p_st_hdr_dst = frlist_create();
    p_st_hdr_src = frlist_create();

    p_data = (char *)frmalloc(strlen("3") + 1);
    snprintf(p_data,2,"%s","3");
    list_node = frlist_node_create(p_data);
    frlist_add_tail(p_st_hdr_src,list_node);

    p_data = (char *)frmalloc(strlen("4") + 1);
    snprintf(p_data,2,"%s","4");
    list_node = frlist_node_create(p_data);
    frlist_add_tail(p_st_hdr_src,list_node);

    frlist_merge_header_top(p_st_hdr_dst, p_st_hdr_src);

    CU_ASSERT_EQUAL(p_st_hdr_dst->size,2);

    list_node = frlist_node_idx(p_st_hdr_dst, 0);
    CU_ASSERT_STRING_EQUAL(list_node->data,"3");

    list_node = frlist_node_idx(p_st_hdr_dst, 1);
    CU_ASSERT_STRING_EQUAL(list_node->data,"4");


    frlist_destroy(p_st_hdr_dst,__my_list_node_free);
    frlist_destroy(p_st_hdr_src,__my_list_node_free);
    return;
}

int add_suite_frlist_merge_header_top(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frlist frlist_merge_header_top()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);

    return FR_SUCC;
}
